package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.FlightBase;
import com.isoft.result.R;
import com.isoft.service.FlightBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flightBase")
public class FlightBaseController {

    @Autowired
    private FlightBaseService flightBaseService;

    /**
     * 分页查询航班基础信息
     */
    @GetMapping("/page")
    public R getPage(@RequestParam(defaultValue = "1") Integer current,
                     @RequestParam(defaultValue = "10") Integer size,
                     @RequestParam(required = false) String awcd,
                     @RequestParam(required = false) String flno,
                     @RequestParam(required = false) String flio,
                     @RequestParam(required = false) String fait) {
        Page<FlightBase> page = new Page<>(current, size);
        QueryWrapper<FlightBase> queryWrapper = new QueryWrapper<>();

        if (awcd != null && !awcd.isEmpty()) {
            queryWrapper.eq("airline_code", awcd);
        }
        if (flno != null && !flno.isEmpty()) {
            queryWrapper.like("flight_number", flno);
        }
        if (flio != null && !flio.isEmpty()) {
            // 根据出发/到达机场判断进出港，这里简化处理
            if ("D".equals(flio)) {
                queryWrapper.isNotNull("departure_airport");
            } else if ("A".equals(flio)) {
                queryWrapper.isNotNull("arrival_airport");
            }
        }
        if (fait != null && !fait.isEmpty()) {
            queryWrapper.eq("status", fait);
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc("create_time");

        IPage<FlightBase> pageResult = flightBaseService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取所有航班基础信息
     */
    @GetMapping("/list")
    public R getList() {
        List<FlightBase> list = flightBaseService.list();
        return R.ok(list);
    }

    /**
     * 根据ID获取航班基础信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        FlightBase flightBase = flightBaseService.getById(id);
        return R.ok(flightBase);
    }

    /**
     * 新增航班基础信息
     */
    @PostMapping
    public R save(@RequestBody FlightBase flightBase) {
        boolean result = flightBaseService.save(flightBase);
        return result ? R.ok("新增成功") : R.error("新增失败");
    }

    /**
     * 更新航班基础信息
     */
    @PutMapping
    public R update(@RequestBody FlightBase flightBase) {
        boolean result = flightBaseService.updateById(flightBase);
        return result ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除航班基础信息
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        boolean result = flightBaseService.removeById(id);
        return result ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 批量删除航班基础信息
     */
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        boolean result = flightBaseService.removeByIds(ids);
        return result ? R.ok("批量删除成功") : R.error("批量删除失败");
    }
}